#include "GameBaseState.h"

#include "userStates.h"
#include "MyGame.h"

GameBaseState::GameBaseState() : BaseState()
{
    score = 0;
    lives = 0;
    stateID = 0;

    pauseText.loadFont("fonts/unispace.ttf",16);
    pauseText.setColour(WHITE);
    pauseText.setBoundaries(Vector2di(0,0),Vector2di(GFX::getXResolution(),GFX::getYResolution()));
    pauseText.setAlignment(CENTRED);

    pauseMenu.setMenuStart(0,150);
    pauseMenu.loadFont("fonts/unispace.ttf", 12);
    pauseMenu.setTextColour(WHITE);
    pauseMenu.setTextSelectionColour(RED);
    pauseMenu.getTextObject()->setBoundaries(Vector2di(0,0),Vector2di(GFX::getXResolution(),GFX::getYResolution()));
    pauseMenu.getTextObject()->setAlignment(CENTRED);
    pauseMenu.addItem(MENU_STRING_ITEM);
    pauseMenu.setMenuItemText("Resume");
    pauseMenu.addItem(MENU_STRING_ITEM);
    pauseMenu.setMenuItemText("Quit");

    GFX::setClearColour(BLACK);
};

GameBaseState::~GameBaseState()
{

}

void GameBaseState::pauseScreen()
{
    GFX::clearScreen();

    pauseText.setPosition(0,40);
    pauseText.print("Paused!");

    pauseMenu.render();
}

// This implemenation does not actually work, you still need to overwrite it with your own
// But it fetches some keys used in all games (Start for example to pause)
// So after calling input->update() you should include a call to this function to have all these inputs progressed
void GameBaseState::userInput()
{
    // This is triggered when the user presses the close button (X) on a PC and ensures it actually closes the window
#ifdef PLATFORM_PC
    if(input->isQuit())
    {
        nullifyState();
        return;
    }
#endif

    // you should always keep this check in here to enable the pause menu
    if (input->isStart())
    {
        pauseToggle();
        input->resetKeys();
        return;
    }
}

void GameBaseState::pauseUpdate()
{
    pauseMenu.update();
}

void GameBaseState::pauseInput()
{
    input->update();

    // Pause menu interaction
    if (input->isUp())
        pauseMenu.menuUp();
    if (input->isDown())
        pauseMenu.menuDown();
    if (input->isA())
    {
        switch (pauseMenu.getSelection())
        {
        case 0:
            pauseToggle();
            break;
        case 1:
            setNextState(STATE_MAIN);
            break;
        }
    }

    if (input->isStart())
        pauseToggle();

    input->resetKeys();
}

void GameBaseState::winGame()
{
    // Progresses to the score screen
    setNextState(STATE_SCORE);
}

bool GameBaseState::checkLives(CRint numLives)
{
    // Just
    if (numLives > 0)
        return true;
    return false;
}

void GameBaseState::loseGame()
{
    // Progresses to the game-over screen
    score = 0;
    setNextState(STATE_GAMEOVER);
}
